﻿//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using NUnit.Framework;
//using System.Data;

//namespace Mbs.Test.Model
//{

//    public class PayPlan
//    {
//        /// <summary>
//        /// 还款期数
//        /// </summary>
//        public int TermNum = 0;

//        /// <summary>
//        /// 
//        /// </summary>
//        public DateTime Date;

//        /// <summary>
//        /// 本期本金
//        /// </summary>
//        public decimal CurCorpus = 0;

//        /// <summary>
//        /// 本期利息
//        /// </summary>
//        public decimal CurAccrual = 0;


//        /// <summary>
//        /// 本期合计
//        /// </summary>
//        public decimal CurTermTotal = 0;


//        /// <summary>
//        /// 截止到本期，支付本金总额
//        /// </summary>
//        public decimal CorpusTotal = 0;

//        /// <summary>
//        /// 截止到本期，支付的利息总额
//        /// </summary>
//        public decimal AccrualTotal = 0;


//        /// <summary>
//        /// 截止到本期，所有支付的本息合计
//        /// </summary>
//        public decimal AllTotal
//        {
//            get { return CorpusTotal + AccrualTotal; }
//        }

//        public void Print()
//        {
//            Console.WriteLine("{0,4}{1,10}{2,10}{3,10}{4,12}{5,12}{6,13}",
//                TermNum,
//                CurCorpus,
//                CurAccrual,
//                CurTermTotal,
//                CorpusTotal,
//                AccrualTotal,
//                AllTotal);
//        }
//    }

//    abstract class LoanBase
//    {
//        public List<PayPlan> Table { get; private set; }

//        public LoanBase(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
//        {
//            Table = GetTable();
//            Calculate(balance, months, date, rates);
//        }

//        protected virtual void Calculate(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
//        {
//        }

//        protected decimal GetMonthRate(DateTime date, int months, KeyValuePair<DateTime, PointF>[] rates)
//        {
//            int i;
//            for (i = rates.Length - 1; i >= 0; i--) if (date >= rates[i].Key) break;
//            return (decimal)(months <= 60 ? rates[i].Value.X : rates[i].Value.Y) / 100 / 12;
//        }

//        protected decimal Round(decimal dec)
//        {
//            return decimal.Round(dec, 2);
//        }

//        protected DateTime NextMonth(DateTime date, int day)
//        {
//            date = date.AddMonths(1);
//            return (date.Day >= day) ? date : new DateTime(date.Year, date.Month,
//              Math.Min(day, DateTime.DaysInMonth(date.Year, date.Month)));
//        }

//        List<PayPlan> GetTable()
//        {
//            return new List<PayPlan>();
//        }
//    }

//    // 等本息法
//    sealed class LoanEq : LoanBase
//    {
//        public LoanEq(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
//            : base(balance, months, date, rates)
//        {
//        }

//        protected override void Calculate(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
//        {
//            decimal baseAmount = 0, totalAmount = 0, totalInterest = 0;
//            decimal monthRate0 = decimal.MinValue, monthAmount = decimal.MinValue;

//            for (int day = date.Day, month = months; month >= 1; month--, date = NextMonth(date, day))
//            {
//                var item = new PayPlan();
//                var monthRate = GetMonthRate(date, months, rates);
//                var interest = Round(balance * monthRate);
//                if (monthRate0 != monthRate) monthAmount = GetMonthAmount(balance, monthRate0 = monthRate, month);
//                baseAmount = monthAmount - interest;
//                balance -= baseAmount;
//                if (month == 1 && balance != 0)
//                {
//                    baseAmount += balance;
//                    interest -= balance;
//                    balance = 0;
//                }
//                totalAmount += monthAmount;
//                totalInterest += interest;

//                item.TermNum = months - month + 1;//
//                item.Date = date;
//                Table.Rows.Add(new object[] { months - month + 1, date, baseAmount, interest, monthAmount, balance, totalAmount, totalInterest });
//            }
//        }

//        decimal GetMonthAmount(decimal balance, decimal monthRate, int months)
//        {
//            double tmp = Math.Pow(1 + (double)monthRate, months);
//            return Round((decimal)((double)balance * (double)monthRate * tmp / (tmp - 1)));
//        }
//    }

//    // 等本金法
//    class LoanDesc : LoanBase
//    {
//        public LoanDesc(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
//            : base(balance, months, date, rates)
//        {
//        }

//        protected override void Calculate(decimal balance, int months, DateTime date, KeyValuePair<DateTime, PointF>[] rates)
//        {
//            decimal baseAmount = Round(balance / months), totalAmount = 0, totalInterest = 0;
//            for (int day = date.Day, month = months; month >= 1; month--, date = NextMonth(date, day))
//            {
//                var monthRate = GetMonthRate(date, months, rates);
//                var interest = Round(balance * monthRate);
//                var monthAmount = baseAmount + interest;
//                balance -= baseAmount;
//                if (month == 1 && balance != 0)
//                {
//                    baseAmount += balance;
//                    monthAmount += balance;
//                    balance = 0;
//                }
//                totalAmount += monthAmount;
//                totalInterest += interest;
//                Table.Rows.Add(new object[] { months - month + 1, date, baseAmount, interest, monthAmount, balance, totalAmount, totalInterest });
//            }
//        }
//    }

//    [TestFixture]
//    public class LoanCalculatorTest
//    {
//        [Test]
//        public void Test()
//        {
//            var cal = new LoanCalculator(LoanCalculator.PayTypeEnum.Equal, 0.04158m, 340000m, 180);
//            var result = cal.Calculate();

//            Console.WriteLine("{0,2}{1,8}{2,8}{3,6}{4,8}{5,8}{6,9}",
//                                "期数","本金","利息","本期合计","本金合计","利息合计","所有合计");

//            foreach (var item in result)
//                item.Print();
//        }
//    }
//}
